package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.dto.template.imp.ImportTemplateListReqDTO;
import com.evil.application.pojo.entity.ApplicationImportTemplate;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.enums.SwitchEnum;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 导入模板表
 *
 * @author liyang
 * @date 2023-04-06 17:56
 */
public interface ApplicationImportTemplateMapper extends BaseMapper<ApplicationImportTemplate> {

    int updateBatch(List<ApplicationImportTemplate> list);

    int batchInsert(@Param("list") List<ApplicationImportTemplate> list);

    default Optional<ApplicationImportTemplate> isExistById(Long id) {
        return Optional.ofNullable(this.selectById(id));
    }

    default ApplicationImportTemplate findById(Long id) {
        return this.isExistById(id).map(e -> {
            if (SwitchEnum.YES.getId() == e.getIsDeleted()) {
                throw new BusinessException(RCodeEnum.APPLICATION_IMPORT_TEMPLATE_DELETED);
            }
            return e;
        }).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_IMPORT_TEMPLATE_NOT_EXIST));
    }

    Optional<ApplicationImportTemplate> isExistByKey(String key);

    default ApplicationImportTemplate findByKey(String key) {
        return this.isExistByKey(key).map(e -> {
            if (SwitchEnum.YES.getId() == e.getIsDeleted()) {
                throw new BusinessException(RCodeEnum.APPLICATION_IMPORT_TEMPLATE_DELETED);
            }
            return e;
        }).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_IMPORT_TEMPLATE_NOT_EXIST));
    }

    List<ApplicationImportTemplate> findByIds(@Param("ids") Collection<Long> ids);

    List<ApplicationImportTemplate> findByEnterpriseId(Long enterpriseId);

    List<ApplicationImportTemplate> findByAppId(Long applicationId);

    List<ApplicationImportTemplate> importTemplateList(ImportTemplateListReqDTO importTemplateListReqDTO);
}
